package com.inet.baidu;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import com.inet.observer.ACCObserver;
import com.inet.receiver.MyBroadcastReceiver;
import com.inet.receiver.MyHandler;
import com.inet.request.ServerRequest;
import com.inet.util.HttpUtil;
import com.inet.util.L;
import com.inet.util.LocusBean;
import com.inet.util.Utils;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;

/**
 * 基于百度SDK的业务操作类
 * 
 * @author YeWen.Xu
 */
public class BaiduService extends Service {

	private IBinder binder = new BaiduService.LocalBinder();

	// 2秒进行一次定位扫描
	private int span = 2 * 1000;

	private MyBroadcastReceiver LowPowerReceiver;
	private ACCObserver mAccObserver;

	public static List<JSONObject> list = new ArrayList<JSONObject>();
	Timer timer = new Timer();
	TimerTask task = new TimerTask() {

		@Override
		public void run() {
			// 执行批量上传点操作
			if (list.isEmpty()) {
				L.i("no need upload point");
				return;
			}
			try {
				ServerRequest.uploadLocusPoint(
						Utils.getImei(getApplicationContext()), list);
			} catch (Exception e) {
			}
			list.clear();
		}
	};

	/**
	 * 初始化百度定位SDK
	 */
	@Override
	public void onCreate() {
		super.onCreate();
		SDKInitializer.initialize(getApplicationContext());
		LocationClient locationClient = new LocationClient(
				getApplicationContext());
		MyLocationListener myLocationListener = new MyLocationListener(
				getApplicationContext());
		locationClient.registerLocationListener(myLocationListener);
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);
		option.setCoorType("bd09ll");
		option.setScanSpan(span);
		option.setIsNeedAddress(true);
		locationClient.setLocOption(option);
		locationClient.start();

		timer.schedule(task, 15 * 60 * 1000, 15 * 60 * 1000);

		// 动态添加广播
		addFilter();

		// 检测ACCON的状态
		mAccObserver=new ACCObserver(new MyHandler(getApplicationContext()),
				getApplicationContext());
		getContentResolver().registerContentObserver(
				android.provider.Settings.System.getUriFor("acc_state"),
				false,mAccObserver);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	public class LocalBinder extends Binder {
		BaiduService getService() {
			return BaiduService.this;
		}
	}

	/**
	 * 动态注册广播 低电
	 */
	private void addFilter() {
		MyHandler mHandler = new MyHandler(getApplicationContext());
		LowPowerReceiver = new MyBroadcastReceiver(mHandler);
		IntentFilter filter = new IntentFilter();
		// filter.addAction(MyBroadcastReceiver.LOW_POWER);
		// filter.addAction(MyBroadcastReceiver.LOW_POWER_OFF);
		filter.addAction(MyBroadcastReceiver.RECEIVE_PICTURE);
		filter.addAction(MyBroadcastReceiver.RECEIVE_VEDIO);
		filter.addAction(MyBroadcastReceiver.ACTION_ACC_ON_KEYEVENT);
		registerReceiver(LowPowerReceiver, filter);

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(LowPowerReceiver);
		getContentResolver().unregisterContentObserver(mAccObserver);
	}
}
